﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2ViewResults
{
    public class ViewResultsActions
    {
        public static int CountNumberOfResult(string strSentimentCategoryItem)
        {
            int intNumber = 0;

            //Using While to wait until there are some results returned
            //it means that there will be the name of the SentimentCategoryItem display on the select list
            Boolean FLAG = true;
            while (FLAG)
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlDiv pnlWrapper = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlWrapper");
                HtmlDiv divReport = pnlWrapper.Find.ById<HtmlDiv>("divReport");
                HtmlSelect ddlLabel = divReport.Find.ById<HtmlSelect>("ddlLabel");

                //Loop over all items on the selectlist to check
                int intCountOptions = ddlLabel.Options.Count;
                for (int i = 0; i < intCountOptions; i++)
                {
                    string strBeingCheckedOption = ddlLabel.Options[i].Text.Trim();
                    string strCheckKeyword = strSentimentCategoryItem;
                    if (strSentimentCategoryItem.Contains(" "))
                    {
                        strCheckKeyword = strSentimentCategoryItem.Replace(" ", " and ");
                    }
                    if (strBeingCheckedOption.Equals(strCheckKeyword))
                    {
                        //If matches, select it
                        SM2Input.Tradition.SelectItemSelectList(ddlLabel, strSentimentCategoryItem);
                        System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                        System.Threading.Thread.Sleep(2000);

                        //Waiting until the the number of result is visible for picking up
                        Boolean FLAG_GOT_NUMBER = true;
                        while (FLAG_GOT_NUMBER)
                        {
                            try
                            {
                                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                                pnlWrapper = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlWrapper");
                                HtmlDiv UpdatePanel1 = pnlWrapper.Find.ById<HtmlDiv>("UpdatePanel1");
                                HtmlSpan lblTotalResults = pnlWrapper.Find.ById<HtmlSpan>("lblTotalResults");
                                if (lblTotalResults.IsVisible())
                                {
                                    string strTotalResults = lblTotalResults.TextContent.Trim();
                                    string strNumber = strTotalResults.Split(' ')[2];
                                    intNumber = int.Parse(strNumber);
                                    Utilities.GlobalObject.LOGGER.log("Get number of results = " + intNumber);
                                    FLAG_GOT_NUMBER = false;
                                }
                            }
                            catch { }
                            System.Threading.Thread.Sleep(2000);
                        }
            
                        //Exit
                        FLAG = false;
                        break;
                    }
                }

                //Reload
                if (FLAG)
                {
                    System.Threading.Thread.Sleep(30000);
                    Utilities.GlobalObject.LOGGER.log("Refresh page ...");
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.Refresh();
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                    Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                }
                else break;
            }

            return intNumber;
        }
    }
}
